//
//  SFGiftLBoViewController.m
//  GiftThat
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "SFGiftLBoViewController.h"
#import "SFGiftOtherCellModel.h"
#import "SFGiftThatTableViewCell.h"
#import "SFSelectionCellModel.h"
#import "SFJumpWebViewController.h"
@interface SFGiftLBoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSNumber *ID;
@property (nonatomic,strong) NSString *IDstring;
@property (nonatomic,strong) NSMutableString *mutablestring;
@property (nonatomic,strong) SFGiftOtherCellModel *GiftOtherCellModel;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) SFSelectionCellModel *SelectionCellModel;


@end

@implementation SFGiftLBoViewController
- (instancetype)initWithModel:(id)newModel
{
    self = [super init];
    if (self) {
   
        self.ID = newModel;
        self.IDstring = [NSString stringWithFormat:@"%@",self.ID];
        self.dataSource = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStyleDone target:self action:@selector(leftButton:)];
    NSString *string = @"http://api.liwushuo.com/v2/collections/205/posts?limit=20&offset=0";
    self.mutablestring = [[NSMutableString alloc]initWithString:string];
    NSRange range = {39,3};
    [self.mutablestring replaceCharactersInRange:range withString:self.IDstring];
    [self GetData];
    [self GetTableView];
    
}
-(void)GetData{
    [CXNetWorkTool getWithUrl:self.mutablestring parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
//        self.GiftOtherCellModel = [result[@"data"] objectForKey:@"paging"];
        NSArray *array = [result[@"data"] objectForKey:@"posts"];
     
        for (NSDictionary *dic in array) {
            self.SelectionCellModel = [[SFSelectionCellModel alloc]initWithDictionary:dic];
            
            [self.dataSource addObject:self.SelectionCellModel];
            
        }
       
        [self.tableview reloadData];
    } fail:^(NSError *result) {
        
    }];
}
//创建ableView
- (void) GetTableView{
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, - 35, self.view.width, self.view.height - 44) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[SFGiftThatTableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self.view addSubview:self.tableview];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SFGiftThatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
    
    [cell setSelectionCellModel:self.dataSource[indexPath.row]];
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SFJumpWebViewController *JumpWebView = [[SFJumpWebViewController alloc]initWithURL:[self.dataSource[indexPath.row] content_url]];
    [self.navigationController pushViewController:JumpWebView animated:YES];
    CATransition *animation = [CATransition animation];
    animation.duration = 1.5f;
    [animation setType:@"kCATransitionFade"];
    
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    
}
- (void)leftButton:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
